<template>
  <div>
    <div class="mb-3">
      <el-button type="primary" @click="edit()" v-show="isRiskPerson">新增报告</el-button>
    </div>
    <loading-wrap>
      <el-table border :data="tableData">
        <el-table-column label="检查表序号" align="center" prop="order_num"></el-table-column>
        <el-table-column label="参与走访人" align="center" prop="visit_persons"></el-table-column>
        <el-table-column label="走访时间" align="center">
          <template slot-scope="scope">
            {{ $utils.dateFormat(scope.row.visit_date, 'yyyy.MM.dd') }}
          </template>
        </el-table-column>
        <el-table-column
          label="客户单位人员"
          align="center"
          prop="customer_persons"
        ></el-table-column>
        <el-table-column label="报告状态" align="center" prop="report_status">
          <template slot-scope="scope">
            {{ enumBehReportStatus[scope.row.report_status] || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="100px">
          <template slot-scope="scope">
            <el-button type="text" class="py-0 px-1" v-show="isRiskPerson" @click="edit(scope.row)">
              <i class="fa fa-edit"></i>
            </el-button>
            <el-button
              type="text"
              class="py-0 px-1 text-danger"
              v-show="isRiskPerson"
              @click="deleteData(scope.row)"
            >
              <i class="fa fa-trash"></i>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </loading-wrap>
  </div>
</template>
<script>
import { enumBehReportStatus } from '@/credit/plugins/enum'
export default {
  components: {},
  props: {},
  data() {
    return {
      tableData: [],
      enumBehReportStatus,
      tableName: 'credit_load_beh_report',
    }
  },
  computed: {
    isRiskPerson() {
      return (
        this.$store.getters.user.job === 'org_r_emp' ||
        this.$store.getters.user.job === 'org_r_manage' ||
        this.$store.getters.user.job === 'org_c_manage' ||
        this.$store.getters.user.job === 'org_credit_super'
      )
    },
  },
  watch: {},
  created() {
    this.id = this.$route.query.id || sessionStorage.getItem('customer_related_id')
    this.getData()
  },
  mounted() {},
  methods: {
    getData() {
      this.$api
        .getData(
          'credit',
          this.tableName,
          {
            'customer_related_id.eq': this.id,
          },
          { order_num: 'desc' }
        )
        .then(res => {
          this.tableData = res.data
        })
    },
    edit(p) {
      this.$router.push({
        path: '/credit/task/after/detail',
        query: {
          rowid: p ? p.credit_load_beh_report_id : '',
        },
      })

      sessionStorage.setItem('report_status', p ? p.report_status : '')
    },
    deleteData(item) {
      const h = this.$createElement
      this.$msgbox({
        title: '提示',
        message: h('span', undefined, `确定删除此报告？`),
        showCancelButton: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        beforeClose: async (action, instance, done) => {
          if (action === 'confirm') {
            instance.confirmButtonLoading = true
            instance.confirmButtonText = '删除中...'
            const [res] = await this.$utils.promise(
              this.$api.deleteData('credit', this.tableName, item.credit_load_beh_report_id)
            )
            instance.confirmButtonLoading = false
            instance.confirmButtonText = '确定'
            if (res) {
              this.$message.success('删除成功！')
              this.getData()
              done()
            } else {
              this.$message.error('删除失败，请重试！')
            }
          } else {
            done()
          }
        },
      }).catch(() => {})
    },
  },
}
</script>
<style scoped></style>
